<?php
/**
 * TinyMVC - TinyMVCPathResolver.class.php file
 *
 * @author Adrian Wieczorek <adi(at)tinymvc(dot)org>
 * @package TinyMVC
 * @version $Id: TinyMVCPathResolver.class.php 48 2008-09-18 16:22:27Z adi3ek $
 *
 */

/**
 * Simple utility class which helps resolving files path
 * @package TinyMVC
 */
class TinyMVCPathResolver {

	private $regExPatterns = array();

 /**
	 * @param string $regExPattern regular expression pattern
	 * @param string $patternKey pattern key (optional)
  */
	public function __construct($regExPattern, $patternKey = 'default') {
	 $this->regExPatterns[$patternKey] = $regExPattern;
	}

	/**
	 * add another pattern
	 * @param string $patternKey pattern key
	 * @param string $regExPattern regular expression pattern
	 */
	public function addPattern($patternKey, $regExPattern) {
		$this->regExPatterns[$patternKey] = $regExPattern;
	}

	/**
	 * get regular expression patern by key
	 * @param string $patternKey pattern key
	 * @return string pattern
	 */
	public function getPattern($patternKey = 'default') {
		return isset($this->regExPatterns[$patternKey]) ? $this->regExPatterns[$patternKey] : null;
	}

	/**
	 * get all patterns
	 * @return array all regular expression patterns
	 */
	public function getPatterns() {
		return $this->regExPatterns;
	}

	/**
	 * resolve file path
	 * @param string $fileName file name
	 * @param string $patternKey pattern key (optional)
	 * @return string absolute file path
	 */
	public function resolve($fileName, $patternKey = 'default') {
		if(isset($this->regExPatterns[$patternKey])) {
			return preg_replace( '#(' . $fileName . ')#', $this->regExPatterns[$patternKey], $fileName);
		}
		else {
			throw new TinyMVCException("Unknown pattern key: $patternKey");
		}
	}
}
